local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseObject());
local CallBackManager = ClientOHOH.load.CallBackManager();
local BtnHelper = ClientOHOH.load.BtnHelper();
local PlayerInfoHelper=_G.ClientOHOH.load.PlayerInfoHelper()

local gCellInsts={
    active="ctrl.active",
    btn="ctrl.btn",
    bg="ctrl.bg",
    deleteIcon="ctrl.deleteIcon",
    equipIcon="ctrl.equipIcon",
    icon="ctrl.icon",
    level="ctrl.level",
    name="ctrl.name",
    head="ctrl.head",
}

function M.new(...)
	return M(...);
end;


function M.ctor(self,ui,data,ctrl)

    self.data=data
    self.ctrl=ctrl
    self.uiObj=ui
    self.ui=ctrl:InitUiInst2(ui,gCellInsts)

    self.click=_G.CommonOHOH.load.Connect().new()

    CallBackManager:AddRbxConnect(self,  self.data.UpdateEvent, function()

        self:Full()
     
    end);
    -- self.data.UpdateEvent:Connect(function()
    --     self:Full()
    -- end)

    self:GuiButton(self.uiObj)

    CallBackManager:AddRbxConnect(self,self.ui.btn.Activated, function()

        self.click:Action(self)
     
    end);
    self:Full()

	M.super.ctor(self);

end;

function M.GuiButton(self,uiObj)

    for _, v in pairs(uiObj:GetDescendants()) do
        if v:IsA("GuiButton") then
            BtnHelper:InitButton(v, nil, nil, nil, function() end);
        end;
    end;

end



function M.Full(self)
    -- print("+++++++++Full++++++++++++++", self.data.id,self.data.userId)


    self.ui.active.Visible=false
    self.ui.bg.BackgroundColor3=self.ctrl:getBgColorByTitleId(self.data.titleId)
    self.ui.deleteIcon.Visible=false

    self:UpdateEquipIconShow()
    self.ui.icon.Image=self.data.icon
    self.ui.level.Text=self.data.level
    self.ui.name.Text=self.data.name
 
    if self.data.userId then
        self.ui.head.Visible=true
        PlayerInfoHelper.GetPlayerInfo(self.data.userId,function(head,name)
            self.ui.head.Image=head
        end)
    else
        self.ui.head.Visible=false;
    end


    --排序
    -- self.uiObj.LayoutOrder=100000-self.data.value+self.data.index
    self.uiObj.LayoutOrder=100000-self.data.value
end

function M.UpdateEquipIconShow(self)
    if self.data.isEquip and self.data.index==1 and  not self.ui.active.Visible then
        self.ui.equipIcon.Visible=true
    else
        self.ui.equipIcon.Visible=false
    end
end


-- -- 加成位数&加成第2位数 xxxY:eg:246(24为位数，6为第一数)
-- function M.GetSortID(v)

--     local sortValue=tostring(math.floor(v*100)) --放大100倍去除小数
--     -- print(len(sortValue))
--     local digit=tostring(#sortValue) --获取位数
--     local first2Digit = string.sub(tostring(sortValue), 1, 2)--获取前2位
--     return 1000-tonumber(digit..first2Digit)

-- end



function M.Show(self)
    self.uiObj.Visible=true
end

function M.Hide(self)
    self.uiObj.Visible=false
end

function M.IsEqual(self,cell)
    local data=cell:GetData()
    return data.weaponId==self.data.weaponId
end


function M.IsEquiped(self)
    return  self.data.isEquip
end


function M.dtor(self)
    self.uiObj:Destroy()
	M.super.dtor(self);
end;

function M.IsSelete(self)
    return self.ui.active.Visible
end;

function M.IsHadRemoveTag(self)
    return  self.ui.deleteIcon.Visible
end

function M.ShowRemoveTag(self)
       self.ui.deleteIcon.Visible=true
end;
function M.HideRemoveTag(self)
     self.ui.deleteIcon.Visible=false
end;

function M.Active(self)
    self.ui.active.Visible=true
    self:UpdateEquipIconShow()
end

function M.NoActive(self)
    self.ui.active.Visible=false
    self:UpdateEquipIconShow()
end

function M.GetData(self)
    return self.data
end


function M.isHadUser(self)
    local data=self:GetData()
    if data.userId then
        return true
    end
    return false
end
function M.GetUi(self)
    return  self.ui
end

return M